#pragma once

#include<iostream>
#include"Socket.hpp"
#include<poll.h>
#include<sys/time.h>

using namespace std;

static const uint16_t defaultport = 8080;
static const int fd_num_max = 64;
int defaultfd = -1; 
int non_event = 0;

class PollServer
{
public:
    PollServer(uint16_t port = defaultport)
    :_port(port)
    {
        for(int i = 0; i<fd_num_max;i++)
        {
            _event_fds[i].fd = defaultfd;
            _event_fds[i].events = non_event;
            _event_fds[i].revents = non_event;
        }
    }

    ~PollServer()
    {}
public:
    bool Init()
    {
        _listensock.Socket();
        _listensock.Bind(_port);
        _listensock.Listen();

        return true;
    }

    void Accepter()
    {
        string clientip;
        uint16_t clientport = 0;
        
        int sock = _listensock.Accept(&clientip, &clientport);
        if(sock < 0) return;
        lg(Info, "accept success, ip: %s, port: %d", clientip.c_str(), clientport);

        // char buffer[1024];
        // ssize_t n = read(sock, buffer, sizeof(buffer));//这里可以直接读吗？不可以
        int pos = 1;
        for(; pos < fd_num_max; pos++)
        {
            if(_event_fds[pos].fd != defaultfd) continue;
            else break;
        }
        if(pos == fd_num_max)
        {
            lg(Warning, "server is full, get new fd faided");
            close(sock);

            //扩容
        }
        else
        {
            _event_fds[pos].fd = sock;
            _event_fds[pos].events = POLLIN;
            _event_fds[pos].revents = non_event;
        }
    }

    void Recver(int fd, int i)
    {
        char buffer[1024];
        ssize_t n = read(fd, buffer, sizeof(buffer)-1);
        if(n > 0)
        {
            buffer[n] = 0;
            cout<<"get a message: "<<buffer<<endl;
        }
        else if(n == 0)
        {
            lg(Info, "client quit, close fd is : %d", fd);
            close(fd);
            _event_fds[i].fd = defaultfd;
            _event_fds[i].events = non_event;
            _event_fds[i].revents = non_event;
        }
        else
        {
            lg(Warning, "read error, close fd is : %d", fd);
            close(fd);
            _event_fds[i].fd = defaultfd;
            _event_fds[i].events = non_event;
            _event_fds[i].revents = non_event;
        }
    }

    void Dispatcher()
    {
        for(int i = 0; i < fd_num_max; i++)
        {
            int fd = _event_fds[i].fd;
            if(fd == defaultfd) continue;

            if(_event_fds[i].revents & POLLIN)
            {
                if(fd == _listensock.GetFd())
                {
                    Accepter();
                }
                else
                {
                    Recver(fd, i);
                }
            }
        }
    }


    void Start()
    {
        int listensock = _listensock.GetFd();
        _event_fds[0].fd = _listensock.GetFd();
        _event_fds[0].events = POLLIN;
        int timeout = 3000;
        // _event_fds[0].revents = non_event;
        while(true)
        {
            int n = poll(_event_fds, fd_num_max, timeout);
            switch(n)
            {
                case 0:
                    cout<<"time out, timeout: "<<timeout<<endl;
                    break;
                case -1:
                    cerr<<"poll error"<<endl;
                    break;
                default:
                    Dispatcher();
                    break;
            }
        }
    }

    void PrintFd()
    {

    }
private:
    Sock _listensock;
    uint16_t _port;
    struct pollfd _event_fds[fd_num_max];
};

